﻿
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using WinFormsApp1.Common;

namespace WinFormsApp1
{
    public partial class ShowDepartmentData : Form
    {
        public ShowDepartmentData()
        {
            InitializeComponent();
            DataColumn dy = new DataColumn();
            //自动增加
            dy.AutoIncrement = true;
            //起始
            dy.AutoIncrementSeed = 1;
            //自动递增主键起始值
            dy.AutoIncrementStep = 1;
            //不允许为空值
            dy.AllowDBNull = true;
            //执行方法
            GetSectionData(new GetShowSectionData());
        }
        /// <summary>
        /// 添加表格和表格数据
        /// </summary>
        public async void GetSectionData(GetShowSectionData L)
        {

            //实例化查询条件
            GetShowSectionData getdata = new();
            getdata.SectionName = L.SectionName;
            getdata.ynum = L.ynum;
            getdata.qnum = L.qnum;
            getdata.count = L.count;
            getdata.znum = L.znum;
            //http://localhost:5106/api/Week01Get/ShowSectionData
            var res = await HttpClientHelper.RequestUrl<GetShowSectionData, ApiResult<ShowSectionData>>(URLCommon.Url2, "/Week01Get/ShowSectionData", HttpClientHelper.MethodType.POST, getdata);
            if (res.Code > 0)
            {
                //实例化表结构
                DataTable dt = new DataTable();
                //往表格里添加第一条数据
                dt.Columns.Add("部门编号", typeof(int));
                dt.Columns.Add("部门名称", typeof(string));
                dt.Columns.Add("企业管理", typeof(string));
                //http://localhost:5106/api/Week01Get/ShowSectionData
                if (res.Data != null)
                {
                    //实例化对象，获取服务端的数据
                    List<SectionData> data = res.Data.data;
                    //循环存入
                    foreach (var item in data)
                    {
                        //新建一行，往新的一行中添加数据
                        dt.Rows.Add(item.SectionId, item.SectionName, item.BusinessName);
                    }
                    getdata.count = res.Data.count;
                    getdata.znum = res.Data.znum;
                    ZCount.Text = getdata.count.ToString();
                    //定义一个存储页数数量的按钮
                    Button YnumData = new Button();
                    int x = 10;
                    int y = 10;
                    for (int i = 0; i < res.Data.znum; i++)
                    {
                        x += 1000;
                        y += 10;
                        YnumData.Text = i.ToString();
                        YnumData.Location = new Point(x, y);
                        YnumData.Size = new Size(100, 50);
                        Controls.Add(YnumData);
                    }

                }
                //往表格中添加数据.
                dataGridView1.DataSource = dt;
                //向控件集里添加控件
                this.Controls.Add(dataGridView1);
            }
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        }
        /// <summary>
        /// 定义从后台获取的数据
        /// </summary>
        public class ShowSectionData
        {
            /// <summary>
            /// 部门数据
            /// </summary>
            public List<SectionData> data { get; set; }
            /// <summary>
            /// 总条数
            /// </summary>
            public int count { get; set; }
            /// <summary>
            /// 总页数
            /// </summary>
            public double znum { get; set; }
        }
        /// <summary>
        /// 显示部门数据
        /// </summary>
        public class SectionData
        {
            /// <summary>
            /// 部门编号
            /// </summary>
            public int SectionId { get; set; } = 0;
            /// <summary>
            /// 部门名称
            /// </summary>
            public string SectionName { get; set; } = string.Empty;
            /// <summary>
            /// 部门类型
            /// </summary>
            public string SectionType { get; set; } = string.Empty;

            /// <summary>
            /// 企业管理（外键）
            /// </summary>
            public int BusinessId { get; set; } = 0;
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsDelete { get; set; } = false;
            /// <summary>
            /// 企业名称
            /// </summary>
            public string BusinessName { get; set; } = string.Empty;
        }
        /// <summary>
        /// 假设是获取部门数据的类
        /// </summary>
        public class GetShowSectionData
        {
            /// <summary>
            /// 部门名称
            /// </summary>
            public string SectionName { get; set; } = "";
            /// <summary>
            /// 取页数
            /// </summary>
            public int ynum { get; set; } = 1;
            /// <summary>
            /// 取条数
            /// </summary>
            public int qnum { get; set; } = 3;
            /// <summary>
            /// 总条数
            /// </summary>
            public int count { get; set; } = 0;
            /// <summary>
            /// 总页数
            /// </summary>
            public double znum { get; set; } = 0;
        }
        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetButton_Click(object sender, EventArgs e)
        {
            //实例化查询条件
            GetShowSectionData getdata = new();
            getdata.SectionName = SerSectionName.Text;
            GetSectionData(getdata);
        }

    }
}
